package com.yaowk.server;

import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.yaowk.authc.interceptor.GuestInterceptor;
import com.yaowk.authc.plugin.AuthcPlugin;
import com.yaowk.authc.routes.AuthcRoutes;
import com.yaowk.authc.task.RemberLoginTask;
import com.yaowk.common.interceptor.Error500Interceptor;
import com.yaowk.common.interceptor.FormDataInterceptor;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.plugin.shiro.ShiroInterceptor;
import com.yaowk.common.plugin.shiro.ShiroPlugin;
import com.yaowk.email.plugin.EmailPlugin;
import com.yaowk.email.routes.EmailRoutes;
import com.yaowk.platform.plugin.PlatformPlugin;
import com.yaowk.platform.routes.PlatformRoutes;
import com.yaowk.system.Interceptor.LogByMethod;
import com.yaowk.system.plugin.SystemPlugin;
import com.yaowk.system.routes.SystemRoutes;
import com.zjh.Guide.plugin.GuidePlugin;
import com.zjh.Guide.routes.GuideRoutes;
import com.zjh.about.plugin.AboutPlugin;
import com.zjh.about.routes.AboutRoutes;
import com.zjh.mailbox.plugin.MailboxPlugin;
import com.zjh.mailbox.server.MailboxRoutes;
import com.zjh.notification.plugin.NotificationPlugin;
import com.zjh.notification.routes.NotificationRoutes;
import com.zjh.technews.plugin.TechnewsPlugin;
import com.zjh.technews.routes.TechnewsRoutes;
import com.zjh.tectpolicy.plugin.TectPolicyPlugin;
import com.zjh.tectpolicy.routes.TectPolicyRoutes;

/**
 * 项目配置文件
 * Created by yaowk on 2017/4/16.
 */
public class ServerConfig extends JFinalConfig {

    private Routes routes;

    public static void main(String[] args) {
        JFinal.start("server/src/main/webapp", 80, "/");
    }

    @Override
    public void configConstant(Constants me) {
//        me.setDevMode(true);

    }

    @Override
    public void configRoute(Routes me) {
        routes = me;
        me.add(new SystemRoutes());
        me.add(new AuthcRoutes());
        me.add(new NotificationRoutes());
        me.add(new AboutRoutes());
        me.add(new GuideRoutes());
        me.add(new TechnewsRoutes());
        me.add(new TectPolicyRoutes());
        me.add(new PlatformRoutes());
        me.add(new EmailRoutes());
        me.add("/api", IndexController.class);
        me.add(new MailboxRoutes());
    }

    @Override
    public void configEngine(Engine me) {

    }

    @Override
    public void configPlugin(Plugins me) {
        // 配置shiro
        me.add(new ShiroPlugin(routes));
        me.add(new EhCachePlugin());
        // 配置多数据源
        Prop prop = PropKit.use("jdbc.properties");
        DruidPlugin druidPlugin = new DruidPlugin(prop.get("jdbcUrl"), prop.get("user"), prop.get("password"), prop.get("driverClass"), prop.get("filters"));
        me.add(druidPlugin);
        // 系统模块数据源
        ActiveRecordPlugin systemArp = new ActiveRecordPlugin(Constant.SYSTEM_MODULE, druidPlugin);
        me.add(new SystemPlugin(systemArp));
        me.add(systemArp);
        // 认证模块数据源
        ActiveRecordPlugin authArp = new ActiveRecordPlugin(Constant.AUTHC_MODULE, druidPlugin);
        me.add(new AuthcPlugin(authArp));
        me.add(authArp);
        ActiveRecordPlugin aboutArp = new ActiveRecordPlugin(Constant.ABOUT_MODULE, druidPlugin);
        me.add(new AboutPlugin(aboutArp));
        me.add(aboutArp);

        ActiveRecordPlugin guideArp = new ActiveRecordPlugin(Constant.GUIDE_MODULE, druidPlugin);
        me.add(new GuidePlugin(guideArp));
        me.add(guideArp);

        ActiveRecordPlugin notificationArp = new ActiveRecordPlugin(Constant.NOTIFICATION_MODULE, druidPlugin);
        me.add(new NotificationPlugin(notificationArp));
        me.add(notificationArp);

        ActiveRecordPlugin technewsArp = new ActiveRecordPlugin(Constant.TECTNEWS_MODULE, druidPlugin);
        me.add(new TechnewsPlugin(technewsArp));
        me.add(technewsArp);

        ActiveRecordPlugin techpolicyArp = new ActiveRecordPlugin(Constant.TECHPOLICY_MODULE, druidPlugin);
        me.add(new TectPolicyPlugin(techpolicyArp));
        me.add(techpolicyArp);

        ActiveRecordPlugin mailboxArp = new ActiveRecordPlugin(Constant.MAILBOX_MODULE, druidPlugin);
        me.add(new MailboxPlugin(mailboxArp));
        me.add(mailboxArp);

        // 平台管理模块数据源
        ActiveRecordPlugin platformArp = new ActiveRecordPlugin(Constant.PLATFORM_MODULE, druidPlugin);
        me.add(new PlatformPlugin(platformArp));
        me.add(platformArp);

        // 文件推送模块数据源
        ActiveRecordPlugin emailArp = new ActiveRecordPlugin(Constant.EMAIL_MODULE, druidPlugin);
        me.add(new EmailPlugin(emailArp));
        me.add(emailArp);


        // 在线人数记录定时器
        Cron4jPlugin cron4jPlugin = new Cron4jPlugin();
        cron4jPlugin.addTask("0 */1 * * *", new RemberLoginTask());
        me.add(cron4jPlugin);

        // 执行模块初始化插件
//        me.add(new SystemInitPlugin());

    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new FormDataInterceptor());
        me.add(new GuestInterceptor());
        me.add(new ShiroInterceptor());
        me.add(new LogByMethod());
        me.add(new Error500Interceptor());
    }

    @Override
    public void configHandler(Handlers me) {

    }
}
